<template>
  <div :class="{ show }">
    <div @click="show = !show" class="right-drawer-background"></div>
    <div class="right-drawer">
      <div class="system-config">
        <h4>系统配置</h4>
        <span @click="show = !show" class="config-close">
          <el-icon><Close /></el-icon>
        </span>
      </div>
      <el-scrollbar>
        <div class="system-config-content">
          <p class="drawer-title">导航模式</p>
          <select-layout-mode></select-layout-mode>
        </div>
      </el-scrollbar>
      <div class="right-drawer-clear">
        <el-button @click="onClearCache" type="danger" text bg>清空缓存</el-button>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from "vue";
import { emitter } from "@/utils/mitt";
import SelectLayoutMode from "./SelectLayoutMode.vue";
import { localForage } from "@/utils/localforage";
import { clearStore } from "@/utils/localStorage";
import { resetRouter } from "@/router";

onMounted(() => {
  emitter.on("openDrawer", () => {
    show.value = true;
  });
});

onBeforeUnmount(() => {
  // 解绑`openDrawer`公共事件，防止多次触发
  emitter.off("openDrawer");
});
const show = ref<boolean>(false);
const onClearCache = async () => {
  console.log("清空缓存");
  clearStore({});
  await localForage().clear();
  resetRouter();
  location.reload();
};
</script>
<style scoped lang="scss">
.el-scrollbar {
  height: calc(100vh - 110px);
}
.right-drawer-background {
  position: fixed;
  top: 0;
  left: 0;
  z-index: -1;
  background: rgb(0 0 0 / 20%);
  opacity: 0;
  transition: opacity 0.3s cubic-bezier(0.7, 0.3, 0.1, 1);
}
.right-drawer {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 40000;
  width: 100%;
  max-width: 280px;
  background-color: #fff;
  box-shadow: 0 0 15px 0 rgb(0 0 0 / 5%);
  transition: all 0.25s cubic-bezier(0.7, 0.3, 0.1, 1);
  transform: translate(100%);
}
.show {
  transition: all 0.3s cubic-bezier(0.7, 0.3, 0.1, 1);

  .right-drawer-background {
    z-index: 20000;
    width: 100%;
    height: 100%;
    opacity: 1;
  }

  .right-drawer {
    transform: translate(0);
  }
}
.system-config {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid rgb(5 5 5 / 6%);
  h4 {
    font-weight: bold;
  }
  .config-close {
    width: 22px;
    height: 22px;
    display: flex;
    align-items: center;
    justify-content: center;
    outline: none;
    cursor: pointer;
    .el-icon {
      width: 18px;
      height: 18px;
      font-weight: bold;
    }
  }
}
.system-config-content {
  padding: 20px;
  .drawer-title {
    margin-bottom: 12px;
    font-size: 14px;
    line-height: 20px;
    font-weight: 500;
  }
}
.right-drawer-clear {
  display: flex;
  justify-content: flex-end;
  border-top: 1px solid rgb(253 253 253 / 12%);
  padding: 12px;
}
</style>
